function Progress_Detail_FillGUI(h)

%Progress_Detail_FillGUI(h) - fills graphic components in GUI Progress_Detail
%Syntax: Progress_Detail_FillGUI(h)
%Description: h is the graphic object handle

handles=guihandles(h);
set(handles.processing_ROC,'Visible','on')
set(handles.processing_pie,'Visible','on')
set(handles.processing_add,'Visible','on')
set(handles.processing_tree,'Visible','on')

%set(handles.member_pie,'Visible','off')
%set(handles.recruitment_label,'Visible','off')
%set(handles.select_model,'Visible','off')
%set(handles.var_addition,'Visible','off')
%set(handles.cv_ROC,'Visible','off')
%set(handles.recruitment,'Visible','off')

dt=get(handles.select_model,'UserData');
i=get(handles.progress_list_models,'Value'); % get time point selected
dt.predict=dt.predict(i);

% Variable recruitment --------  START -------------
axes(handles.recruitment)
[lala,m]=min(mean(dt.predict.dy)); %optimal number of variables
vars=dt.predict.var(:,1:m);Ind=[];
nvars=length(vars(:));
maxf=0; %collect maximum frequency
for i=1:m
    [U{i},ii,jj]=unique(vars(:,i));
    Ind=[Ind,jj];
    %sort U{i} by frequency
    F=[];%frequency
    for j=1:length(U{i})
        F=[F,sum(vars(:,i)==U{i}(j))];
    end
    [lala,ii]=sort(-F);
    if max(F)>maxf;maxf=max(F);end
    U{i}=U{i}(ii);
end

N=round(maxf*1.5);
for i=1:m %for each additional recruit
    MaxVar=0;
    for j=1:length(U{i})
        plot(j,i,'+k','MarkerSize',4);hold on
        if length(U{i})>MaxVar;MaxVar=length(U{i});end
    end
    hold on
    %text(0,i,num2str(i))
    if i>1 %connect the dots
        Mam=[1:length(U{i-1})]-1/N;
        Mpm=[1:length(U{i})]-1/N;
        for j=1:length(vars(:,1)) %for th ith member of the jth panel
            % coming from
            Iam=find(U{i-1}==vars(j,i-1));Mam(Iam)=Mam(Iam)+1/N;
            Ipm=find(U{i}==vars(j,i));Mpm(Ipm)=Mpm(Ipm)+1/N;
            %plot([Mam(Iam),Mpm(Ipm)],[i-1,i],'-','Color',[0.6 0.6 0.6]);
            plot([Mam(Iam),Mpm(Ipm)],[i-1,i],'-','Color',[0.75 0.75 0.75]);
        end
    end
    text(0.9,i,num2str(i),'FontSize',7,'Color','k')
end
% Crop variable names
for i=1:length(dt.vars)
    if length(dt.vars{i})>10
        dt.vars{i}=[dt.vars{i}(1:8),'...']; %<<<< enable to control size of variable names
    end
end

for i=1:m
    mm=length(U{i});    
    for j=1:mm
        %txt=dt.vars{U{i}(j)};
        fs=max([10-5*((i+j)/(m+mm)),10/mm]); % font size
        %fs=min([10,(i+j)/mm]); % font size
       % if length(txt)>10
            %text(j,i,[' ',txt(1:8),'...'],'Color','k','FontWeight','bold','Rotation',40,'FontSize',fs); %'FontAngle','italic',
        %else
            text(j,i,[' ',dt.vars{U{i}(j)}],'Color','k','FontWeight','bold','Rotation',40,'FontSize',fs); %'FontAngle','italic',
        %end
    end
end
%text(MaxVar,1,[num2str(dt.predict.T),'\newlinemonths'],'EdgeColor',[0.75,0.75,0.75],'BackgroundColor',[0.85,0.85,0.85],'HorizontalAlignment','left','FontAngle','italic')
hold off
G=get(gcf);
set(G.Children(1),'YDir','reverse','XScale','log','YScale','log');
if length(vars(1,:))>1
    axis([0.8,MaxVar,1,length(vars(1,:))])
end
axis off
%text(1,m+1,[num2str(length(dt.predict.var(:,1))),' committees (lines), each one with ',num2str(m),'members'])
%title([num2str(dt.predict.T),'\newlinemonths'],'FontSize',9,'Horiz
%ontalAlignment','left')
hold on
for i=1:m
    MaxVar=0;
    for j=1:length(U{i})
        plot(j,i,'dk','MarkerSize',4,'MarkerFaceColor','k');%plot(j,i,'ok','MarkerSize',2,'MarkerFaceColor','k');
        if length(U{i})>MaxVar;MaxVar=length(U{i});end
    end
end
hold off
G=get(handles.recruitment);lala=G.XLim;set(handles.recruitment,'XLim',[lala(1),lala(2)+1]); % Make X axis start at 0

set(handles.recruitment,'Tag','recruitment');
set(handles.recruitment_label,'String',[num2str(length(dt.predict.var(:,1))),' committees (lines), each one with ',num2str(m),' members'])
set(handles.processing_tree,'Visible','off')
% Variable recruitment --------  END -------------


% VARIABLE ADDITION   --------  BEGIN -------------
%disp(':-)');
axes(handles.var_addition)
n=length(dt.predict.var(:,1));
semilogx(100*mean(dt.predict.dy)/n,'o','MarkerFaceColor','y');
%Mark best panel
[lala,Ind]=min(mean(dt.predict.dy));
hold on
semilogx(Ind,100*mean(dt.predict.dy(:,Ind))/n,'o','MarkerFaceColor','k');

%figure;semilogx(sum(z.predict.err),'o','MarkerFaceColor','y');
xlabel('Variable addition')
ylabel('% Wrong Neighbors\newline ')
grid on
set(handles.var_addition,'Tag','var_addition','YAxisLocation','right','FontName','Verdana','FontSize',8);
G=get(handles.var_addition);GYLabel=get(G.YLabel);
%YLabel_Position=GYLabel.Position;%YLabel_Position(1)=450;
set(G.YLabel,'Rotation',270,'FontName','Verdana','FontSize',8,'VerticalAlignment','baseline')
set(G.XLabel,'FontName','Verdana','FontSize',8);
%get positive outcomes
dt.x(dt.predict.exclude,:)=[];
dt.nx(dt.predict.exclude,:)=[];
dt.y(dt.predict.exclude,:)=[];
%y=dt.y;
dt.y=(dt.y<dt.predict.T);
mj=100.*min([n-sum(dt.y),sum(dt.y)])/n; % <-- error that would be obtained by using majority outcome
plot(G.XLim,[mj mj],'r'); % <--- mark that level with a red line
hold off
set(handles.var_addition,'Tag','var_addition')
set(handles.processing_add,'Visible','off')
% VARIABLE ADDITION   --------  END ------------


% MEMBER PIECHARD  --------  START -------------

clear lala
axes(handles.member_pie)
vars=dt.predict.var(:,1:m);vars=vars(:); %all variables
U=unique(vars); n=length(U);%unique variables
F=zeros(n,1); % Counting their occurrence
hh=guihandles(h);
GC=get(hh.var_addition,'Children');
mj=get(GC(1),'YData'); % <-- error that would be obtained by using majority outcome
var_add_w=[mj(1),get(GC(3),'YData')]; % rofile of % of wrong neighbors
var_add_w=var_add_w(1:m)-var_add_w(2:m+1);
var_add_w=repmat(var_add_w,size(dt.predict.var,1),1);var_add_w=var_add_w(:);var_add_w(find(var_add_w<=0))=1;
for i=1:n
    %F(i,1)=sum(vars==U(i)); %old version where each occurrence is equally important
    %new version weighted for decrease in % wrong neighbors
    F(i,1)=sum((vars==U(i)).*var_add_w);
end
[lala,Ind]=sort(-F); U=U(Ind);F=F(Ind);% Sort them by highest frequency
S.vars=dt.vars(U);S.s=F';
maxVars=min([12,length(U)]);
if maxVars<12
    pie(F,dt.vars(U));    
else
    maxVars=10;
    pie([F(1:maxVars);sum(F(maxVars+1:end))],[dt.vars(U(1:10)),'others']);
end
set(handles.member_pie,'Tag','member_pie','UserData',S)
G=get(handles.member_pie);
n=length(G.Children)/2;
for j=1:n-1
    i=n*2-j*2+1;
   set(G.Children(i),'FontSize',9,'Rotation',90+360*(sum(F(1:j))-F(j)/2)/sum(F),'HorizontalAlign','left')
end
j=n; i=n*2-j*2+1;set(G.Children(i),'FontSize',9,'Rotation',90+360*(sum(F)-sum(F(n:end)/2)/sum(F)),'HorizontalAlign','left')

set(handles.processing_pie,'Visible','off')

% MEMBER PIECHARD  --------  END -------------



% ROC  --------  START ------------
axes(handles.cv_ROC)
cvroc(dt.y,neighbor_predict_within(dt),3);
set(handles.cv_ROC,'Tag','cv_ROC')
set(handles.processing_ROC,'Visible','off')
% ROC  --------  END ------------
